package router

import (
	"github.com/gin-contrib/cors"
	"github.com/gin-gonic/gin"
	"guoquan/config"
	"guoquan/controller/v1/login"
	"guoquan/controller/v1/query"
	"guoquan/controller/v1/user"
	"guoquan/router/middleware"
	"guoquan/task"
	"time"
)

var router *gin.Engine

func init() {
	config.InitConfig()
	task.InitTask()
	router = gin.Default()
	router.Use(cors.New(cors.Config{
		AllowOriginFunc:  func(origin string) bool { return true },
		AllowMethods:     []string{"OPTIONS", "GET", "POST", "PUT", "DELETE", "PATCH"},
		AllowHeaders:     []string{"Origin", "Content-Length", "Content-Type", "Authorization"},
		AllowCredentials: true,
		MaxAge:           12 * time.Hour,
	}))

	// 模块路由地址
	loginRoute("login")
	userRoute("user")

	// 请求示例
	queryRoute("query")

	_ = router.Run(config.Run.Port)
}

func loginRoute(name string) {
	route := router.Group(name)
	route.Any("index", login.UserLogin)
}

func userRoute(name string) {
	route := router.Group(name)
	route.Use(middleware.JWTAuth())
	route.Any("list", user.UserList)
}

func queryRoute(name string) {
	route := router.Group(name)
	route.Any("default", query.Default)
	route.Any("array", query.QueryArr)
	route.Any("map", query.QueryMap)
	route.Any("json", query.QueryJson)
}
